package com.jf.util;

import org.apache.commons.codec.digest.DigestUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.UUID;

public class MyUtils {
    public static boolean isStringEmpty(String str){
        return str == null || str.isEmpty();
    }

    public static boolean isStringNotEmpty(String str){
        return !isStringEmpty(str);
    }

    public static Integer string2Int(String str,Integer defaultValue){
        if(isStringEmpty(str)){
            return defaultValue;
        }
        return Integer.parseInt(str);
    }

    public static Integer string2Int(String str){
        return string2Int(str,null);
    }

    public static int[] string2IntArray(String str,String separator){
        String[] strs = string2Array(str,separator);
        if(strs == null){
            return null;
        }
        int[] ints = new int[strs.length];
        for(int i=0;i<ints.length;i++){
            ints[i]=Integer.parseInt(strs[i]);
        }
        return ints;
    }

    public static int[] string2IntArray(String str){
        return string2IntArray(str,",");
    }

    public static String[] string2Array(String str,String separator){
        if(isStringEmpty(str)){
            return null;
        }
        return str.split(separator);
    }
    public static String[] string2Array(String str){
        return string2Array(str,",");
    }

    public static String MD5(String str){
        return DigestUtils.md5Hex(str);
    }

    public static String getFileName(){
        return MD5(UUID.randomUUID().toString());
    }

    public static String getUUID(){
        return getFileName();
    }

    public static String getRequestJson(BufferedReader in){
        String result = "";
        try{
            String content = null;
            while ((content = in.readLine()) != null){
                result+=content;
            }
        }catch (IOException e){
            e.printStackTrace();
        }
        return result;
    }

    public static String getFileType(String fileName){
        String[] fns = fileName.split("\\.");
        return fns[fns.length-1];
    }

}
